import apiClient from './client'
import type { Order, CreateOrderPayload } from '@/types'
import type { PageInfo } from '@/types'

export const orderService = {
  // ===== 订单管理模块 (/api/v1/orders) =====

  // 创建订单
  createOrder: (orderData: CreateOrderPayload) =>
    apiClient.post<Order>('/api/v1/orders/create', orderData),

  // 根据订单ID查询订单详情
  getOrderDetail: (orderId: number) => apiClient.get<Order>(`/api/v1/orders/${orderId}`),

  // 根据订单编号查询订单详情
  getOrderDetailBySn: (orderSn: string) => apiClient.get<Order>(`/api/v1/orders/sn/${orderSn}`),

  // 根据用户ID查询订单列表
  getUserOrders: (userId: number) => apiClient.get<Order[]>('/api/v1/orders/user/' + userId),

  // 根据状态查询订单列表
  getOrdersByStatus: (status: number) => apiClient.get<Order[]>('/api/v1/orders/status/' + status),

  // 分页查询所有订单（支持状态过滤）
  getOrdersList: (
    page: number = 1,
    size: number = 10,
    status?: number,
    startDate?: string,
    endDate?: string,
  ) =>
    apiClient.get<PageInfo<Order>>('/api/v1/orders/list', {
      params: { page, size, status, startDate, endDate },
    }),

  // 搜索订单
  searchOrders: (keyword?: string, status?: number, page: number = 1, size: number = 10) =>
    apiClient.get<PageInfo<Order>>('/api/v1/orders/search', {
      params: { keyword, status, page, size },
    }),

  // 更新订单状态
  updateOrderStatus: (orderId: number, status: number) =>
    apiClient.put(`/api/v1/orders/${orderId}/status`, { status }),

  // 取消订单
  cancelOrder: (orderId: number) => apiClient.put(`/api/v1/orders/${orderId}/cancel`),

  // 确认收货
  confirmReceipt: (orderId: number) => apiClient.put(`/api/v1/orders/${orderId}/confirm`),

  // 申请退货
  applyRefund: (orderId: number) => apiClient.put(`/api/v1/orders/${orderId}/refund/apply`),

  // 处理退货
  processRefund: (orderId: number) => apiClient.put(`/api/v1/orders/${orderId}/refund/process`),

  // 删除订单
  deleteOrder: (orderId: number) => apiClient.delete(`/api/v1/orders/${orderId}`),

  // 获取订单统计信息
  getOrderStatistics: () => apiClient.get<Record<string, number>>('/api/v1/orders/statistics'),
}

export default orderService
